# encoding: utf-8
'''
Created on 2018-8-28
desription:数据库连接助手类， for sqlserver
@author: szy
'''

import pymssql
import sys
import time

#sys.path.append('./helper')
from helper import InfoMonConstants
#import InfoMonConstants
import threading

class ConnectionHelper:
    conn = None
    _l = None
    def __init__(self,dbName):
        self._l = threading.Lock()
        self.conn = pymssql.connect(host=InfoMonConstants.HOST, user=InfoMonConstants.USER, password=InfoMonConstants.PASSWORD, port=InfoMonConstants.PORT, charset="utf8", database=InfoMonConstants.DBNAME)

    # def getCursor(self):
    #     return self.conn.cursor()
    def getValue(self, query):
        self._l.acquire()
        cursor = self.conn.cursor()
        cursor.execute(query)
        result = cursor.fetchall()
        self._l.release()
        return result

    def getOne(self, query):
        self._l.acquire()
        cursor = self.conn.cursor()
        cursor.execute(query)
        result = cursor.fetchone()
        self._l.release()
        return result

    def closeConn(self):
        self.conn.close()


